package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @PostMapping("/add")
    public R<String> addShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        log.info(shoppingCart.toString());

        //判断购物车表里是不是由这个商品  通过setmeal_id去查询
        LambdaQueryWrapper<ShoppingCart> qw=new LambdaQueryWrapper<>();
        qw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        qw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        ShoppingCart cart = shoppingCartService.getOne(qw);
        Long userId = (Long) session.getAttribute("user");
        //cart不等于null 并且cart的userid等于登录用户的id  那么这张表里面的用户数据存在  仅仅需修改数据即可
        if (cart!=null&&cart.getUserId().toString().equals(userId.toString())){
            Integer number = cart.getNumber();
            cart.setNumber(number+1);
//            //根据dishId查询单品价格
//            BigDecimal price=new BigDecimal("0");
//
//            if(cart.getDishId()!=null){
//                Dish dish=dishService.getDishDto(cart.getDishId());
//                price = dish.getPrice().divide(BigDecimal.valueOf(100));
//            }else if (cart.getSetmealId()!=null){
//                Setmeal setmeal=setmealService.selectById(cart.getSetmealId());
//                price = setmeal.getPrice().divide(BigDecimal.valueOf(100));
//            }


            //number只能从数据库拿  不然会出现空指针异常
//
//            BigDecimal amount1= BigDecimal.valueOf(Double.parseDouble(price.toString())*Integer.parseInt(cart.getNumber().toString()));
//            cart.setAmount(amount1);
            shoppingCartService.updateById(cart);
            return R.success("添加成功！");
        }

        //cart为空或者userid不相等  那么该菜品需要重新增加  直接在数据库添加即可
        shoppingCart.setUserId(userId);
        shoppingCartService.save(shoppingCart);
        return R.success("添加成功！");
    }

    //购物车列表的删除
    @PostMapping("/sub")
    public R<String> subShoppingCart(@RequestBody ShoppingCart shoppingCart,HttpSession session){

        log.info(shoppingCart.toString());
        Long user = (Long) session.getAttribute("user");
        log.info("user"+user.toString());
        //查询购物车
        LambdaQueryWrapper<ShoppingCart> qw=new LambdaQueryWrapper();
        qw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        qw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        qw.eq(ShoppingCart::getUserId,user);
        ShoppingCart one = shoppingCartService.getOne(qw);
        //获得完原有的数据以后开始修改number
        if (one!=null&&one.getNumber().toString().equals("1")){
            //如果number==1 直接删除
            shoppingCartService.remove(qw);
            //不需要修改amount
            return R.success("修改成功!");
        }else if(one!=null){
            Integer s=one.getNumber();
            //如果不等于1 那么就直接修改该订单
            one.setNumber(one.getNumber()-1);
//            //修改amount  number只能从数据库拿  不然会出现空指针异常
//            one.setAmount(one.getAmount().subtract(one.getAmount().divide(BigDecimal.valueOf(s))));
            shoppingCartService.updateById(one);
            return R.success("修改成功！");
        }

        return R.error("未知错误！");

    }

    //清空购物车列表
    @DeleteMapping("clean")
    public R<String> cleanShoppingCart(HttpSession session){
        LambdaQueryWrapper<ShoppingCart> qw=new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,(Long)session.getAttribute("user"));
        shoppingCartService.remove(qw);
        return R.success("清空成功！");
    }

    //购物车列表的展示
    @GetMapping ("/list")
    public R<List<ShoppingCart>> listShoppingCart(){
        List<ShoppingCart> list=shoppingCartService.list();
        return R.success(list);
    }

}
